`
https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/
`

/**
 * @param {number[]} dist
 * @param {number} hour
 * @return {number}
 */
var minSpeedOnTime = function (dist, hour) {
  // 思路：https://leetcode.cn/problems/minimum-speed-to-arrive-on-time/solutions/791209/bi-mian-fu-dian-yun-suan-de-xie-fa-by-en-9fc6/
  const n = dist.length;
  const h100 = Math.round(hour * 100);
  const delta = h100 - (n - 1) * 100;
  if (delta <= 0) { // 无法到达终点
    return -1;
  }

  const maxDist = Math.max(...dist);
  if (h100 <= n * 100) { // 特判
    // 见题解中的公式
    return Math.max(maxDist, Math.ceil(dist[n - 1] * 100 / delta));
  }

  function check(v) {
    let t = 0;
    for (let i = 0; i < n - 1; i++) {
      t += Math.ceil(dist[i] / v);
    }
    return (t * v + dist[n - 1]) * 100 <= h100 * v;
  }

  let left = Math.ceil(_.sum(dist) * 100 / h100) - 1; // 也可以初始化成 0（简单写法）
  const h = Math.floor(h100 / (n * 100));
  let right = Math.ceil(maxDist / h); // 也可以初始化成 maxDist（简单写法）
  while (left + 1 < right) {
    const mid = Math.floor((left + right) / 2);
    if (check(mid)) {
      right = mid;
    } else {
      left = mid;
    }
  }
  return right;
};